package cc.douquan.com.doutu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import net.youmi.android.AdManager;
import net.youmi.android.normal.banner.BannerManager;
import net.youmi.android.normal.banner.BannerViewListener;
import net.youmi.android.normal.spot.SpotListener;
import net.youmi.android.normal.spot.SpotManager;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showAd();
        showCp();
        findViewById(R.id.change_ad).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAd();
            }
        });

    }

    @Override
    public void onBackPressed() {
        // 如果有需要，可以点击后退关闭插播广告。
        if (SpotManager.getInstance(MainActivity.this).isSpotShowing()) {
            SpotManager.getInstance(MainActivity.this).hideSpot();
        }
    }

    private void showAd() {
        AdManager.getInstance(getApplication()).init("9d6ba50b15cfef26", "235b403d6188201d", false, true);
        View bannerView = BannerManager.getInstance(this)
                .getBannerView(new BannerViewListener() {
                    @Override
                    public void onRequestSuccess() {

                    }

                    @Override
                    public void onSwitchBanner() {

                    }

                    @Override
                    public void onRequestFailed() {

                    }
                });

        // 获取要嵌入广告条的布局
        LinearLayout bannerLayout = (LinearLayout) findViewById(R.id.bannerContainer);

        // 将广告条加入到布局中
        bannerLayout.addView(bannerView);

    }

    private void showCp() {
        findViewById(R.id.ad).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SpotManager.getInstance(MainActivity.this).setImageType(SpotManager.IMAGE_TYPE_VERTICAL);
                SpotManager.getInstance(MainActivity.this).setAnimationType(SpotManager.ANIMATION_TYPE_ADVANCED);
                SpotManager.getInstance(MainActivity.this).showSpot(MainActivity.this,
                        new SpotListener() {
                            @Override
                            public void onShowSuccess() {

                            }

                            @Override
                            public void onShowFailed(int i) {

                            }

                            @Override
                            public void onSpotClosed() {

                            }

                            @Override
                            public void onSpotClicked(boolean b) {

                            }
                        });
            }
        });
    }
}
